package cn.elegent.txdemo.order.rollback;

import cn.elegent.tx.annotation.RollBack;
import cn.elegent.tx.context.Snapshot;
import cn.elegent.tx.core.rollback.RollBackHandler;
import cn.elegent.txdemo.order.service.OrderService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;

/**
 * 订单回滚
 * @description
 * @author WGL
 * @date 2022/11/8 11:42
*/
@RollBack(mainName = "pay",childName = "order")
@Slf4j
public class OrderRollBack implements RollBackHandler {

    @Autowired
    private OrderService orderService;

    @Override
    public void doRollBack(String topic, Snapshot snapshot) {
        log.info("收到了回滚的消息来自："+topic+",数据："+snapshot.getData());
        Integer orderId = snapshot.findData("orderId");
        orderService.updateOrderStatus(orderId.longValue(),0); //状态改为无效
    }
}
